<?php

namespace app\index\validate;

use app\index\service\CertService;
use app\index\service\OptService;
use think\Validate;

class CertPerson extends Validate
{

    protected $rule = [
        'id' => 'require|number',
        'name' => 'require|check_name',
        'deptid' => 'require',
        'state' => 'require',
        'social' => 'require|check_social',
        'xueli' => 'check_xueli',
        'idcard' => 'check_idcard',
        'outdt' => 'check_outdt',
        'nature' => 'check_nature',
//        'certpose' => 'check_certpose'
    ];

    protected $message = [
        'id' => '网络错误',
        'name' => '请填写人员姓名',
        'deptid' => '请选择所属单位',
        'state' => '请选择状态',
        'social' => '请选择社保状态',
    ];

    protected $scene = [
        'add' => ['name','deptid','state','social','xueli','idcard','outdt','nature','certpose'],
        'edit' => ['id','name','deptid','state','social','xueli','idcard','outdt','nature','certpose']
    ];

    public function check_name($value,$rule,$data){
        $id = isset($data['id']) ? $data['id'] : 0;
        if((new CertService())->check_cert_person_name($value,$id)){
            return '人员姓名已存在';
        }
        return true;
    }

    public function check_idcard($value,$rule,$data){
        $id = isset($data['id']) ? $data['id'] : 0;
        if(!(new CertService())->check_person_idcard_mate($value)){
            return '人员身份证号码错误';
        }
        if((new CertService())->check_person_idcard_exist($value,$id)){
            return '人员身份证号码已存在';
        }
        return true;
    }

    public function check_outdt($value){
        if($value > date("Y-m-d")){
            return '离职日期不能大于当前日期';
        }
        return true;
    }

    public function check_xueli($value){
        if(!(new OptService())->check_option_exists('person_xueli',$value)){
            return '学历不存在请重新选择';
        }
        return true;
    }

    public function check_social($value){
        if(!(new OptService())->check_option_exists('person_social',$value)){
            return '社保不存在请重新选择';
        }
        return true;
    }

    public function check_nature($value){
        if(!(new OptService())->check_option_exists('person_nature',$value)){
            return '人员性质不存在请重新选择';
        }
        return true;
    }

//    public function check_certpose($value){
//        if(!(new OptService())->check_option_exists('person_certpose',$value)){
//            return '证书用途不存在请重新选择';
//        }
//        return true;
//    }

}

